<?php 
namespace Home\Controller;
use Home\Controller;

class CartController extends BaseController{
	//展示购物车主页
	public function index(){
		if($_SESSION['username']==""){
			$this->error('请先登录!',U('Home/User/login'),2);
		}
		$res=M('goods')->select();
		$user=$_SESSION['username'];
		$list=M('ptype')->where(array('user'=>$user,'status'=>1))->select();
		$this->assign('res',$res);
		$this->assign('list',$list);
		$this->display();
	}
	//清空购物车
	public function sclean(){
		$user=$_SESSION['username'];
		$res=M('ptype')->where(array('user'=>$user))->delete();
		$r=array();
		if($res){			
			$r['status']=true;
		}else{
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}
	//单条删除
	public function del(){
		$user=$_SESSION['username'];
		$uid=I('get.uid');
		$res=M('ptype')->where(array('sid'=>$uid))->delete();
		$r=array();
		if($res){			
			$r['status']=true;
		}else{
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}
	//获取地域三级联动
	public function paied(){
		$parent_id=I('get.parent_id');
		$arr=M('areas')->where(array('parent_id'=>$parent_id))->select();
		echo json_encode($arr);		
	}
	//订单制作页面
	public function sess(){
		$arr=$_POST['num'];
		foreach($arr as $k=>$v){
			mysql_query("update ego_ptype set num='{$v}' where sid='{$k}'");
		}
		$where['sid']=array('in',$_POST['ckb']);
		$res=M('paied')->where($where)->select();
		$all=0;
		foreach($res as $k=>$v){
			$all+=$v['goods_price']*$v['num'];
		}
		$_SESSION['total']=$all;
		$_SESSION['payorder']=$res;
		$uid=$_SESSION['uid'];
		$adst=M('addr')->where(array('uid'=>$uid))->select();
		$this->assign('adst',$adst);
		$this->assign('all',$all);
		$this->assign('list',$res);
		$this->display();
	}
	//添加地址信息
	public function addaddr(){
		$addr=I('post.area').I('post.addrs');
		$_POST['addr']=$addr;
		$_POST['uid']=$_SESSION['uid'];
		$user=M('addr');
		$user->create();
		$r=array();
		$res=$user->add();
		$last_id = $user->getLastInsID();
		if($res){			
			$r['last_id']=$last_id;
			$r['status']=true;
		}else{
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}
	//修改地址信息
	public function editaddr(){
		$user=M('addr');
		$user->create();
		$res=$user->where(array('aid'=>I('post.aid')))->save();
		$r=array();
		if($res){			
			$r['status']=true;
		}else{
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}
	//地址删除
	public function tdel(){
		$uid=I('post.tuid');
			$res= M('addr')->where("aid={$uid}")->delete();		
			if($res){
				$re['msg']='删除成功';
				$re['status']=true;		
			}else{
				$re['msg']='删除失败';
				$re['status']=false;	
			}
		$this->ajaxReturn($re);
	}
//生成订单,提交数据库
	public function refer(){ 
		$addr_id = I('post.addr_id');
		$user=$_SESSION['username'];
		$arr=array();
		$res = M("addr")->where(array('aid'=>$addr_id))->find();
		$da['content']=json_encode($_SESSION['payorder']);
		$da['ordnum']='TS'.time().rand(100,999);
		$da['ord_name']=$res['aname'];
		$da['ord_tel']=$res['tel'];
		$da['ord_addr']=$res['addr'];
		$da['user_name']=$_SESSION['username'];
		$da['user_uid']=$_SESSION['uid'];
		$da['total']=$_SESSION['total'];
		$da['ordtime']=time();
		$r=M('order')->add($da);
		$oid=M('order')->getLastInsID();
			if($r){
				foreach($_SESSION['payorder'] as $v){
					M('ptype')->where(array('sid'=>$v['sid']))->delete();
				}
				$re['id']=$da['ordnum'];
				$re['oid']=$oid;
				$re['status']=true;		
			}else{
				$re['status']=false;	
			}
		$this->ajaxReturn($re);
		
	}
//订单支付页面
	public function over(){
		$oid=I('get.oid');
		$res=M('order')->where("oid=$oid")->find();	
		$this->assign('list',$res);
		$this->display();
	}
//付款操作
	public function account(){
		$oid=I('post.oid');
		$res=M('order')->where("oid=$oid")->find();
		$userid=$_SESSION['userInfo']['uid'];
		$total=$_SESSION['userInfo']['money'];
		$da['money']=$total-$res['total'];
		$das['effect']=2;
		if($total<$res['total']){
			$re['msg']="余额不足";
			$re['status']=false;
		}else{
			M('user')->where("uid=$userid")->save($da);
			M('order')->where("oid=$oid")->save($das);
			$re['status']=true;
		}
		$this->ajaxReturn($re);
	}


	
}














 ?>